// 所有的数据请求都在这里执行！

import axios from "axios";
import $axios from "./http"
import store from "@/store"
import { Notify } from "vant"

// 首页数据请求

export async function getIndex(){
    let p1 = $axios.get("/getbanner");
    let p2 = $axios.get("/getseckill");
    let p3 = $axios.get("/getindexgoods");
    let res =await axios.all([p1,p2,p3]);
    return [res[0].list, res[1].list, res[2].list];
}


export async function getCateTree(){
    let res = await $axios.get("/getcate");
    return res.list;
}



export async function getGoodList(id,page=1,size=10) {
    let res = await $axios.get(`/getcategoods?sid=${id}&page=${page}&size=${size}`);
    return res.list.list;
}

export async function getGoodInfo(id) {
    let res = await $axios.get("/getgoodsinfo",{ params:{id} });
    return res.list[0];
}


export async function login(data) {
    let res = await $axios.post("/login",data);
    return res.list;
}

export async function reg(data) {
    let res = await $axios.post("/register",data);
}


// 购物车数据请求
export async function getCarList(){
    let res = await $axios._get('/cartlist',{uid:store.state.user.userinfo.uid});
    if(res.list){
        return res.list
    }else{
        return [];
    }
}
export async function addCar(goodsid,num) {
    if(goodsid && num){
        let res = await $axios._post('/cartadd', {
            uid: store.state.user.userinfo.uid,
            goodsid,
            num
        });
        if(res.code==200){
            Notify({ type: 'success', message: "加入成功" });
        }else{
            Notify({ type: 'danger', message: "加入失败" });
        }
    }else{
        Notify({ type: 'danger', message: "数量和请求id需要传入"});
    }
   
}
export async function editCar(id, type) {
    let res = await $axios._post('/cartedit', {
       id,type
    });
    console.log(res)
}
export async function delCar(id) {
    let res = await $axios._post('/cartdelete', {
        id
    });
    if (res.code == 200) {
        Notify({ type: 'success', message: "删除成功" });
    } else {
        Notify({ type: 'danger', message: "删除失败" });
    }
}



